/*********************/
/** ProcessRFID.java */
/*********************/
package atag.apps.mainApp;

import atag.runtime.DataItem;
import atag.runtime.DataPool;
import atag.runtime.NodeInfo;
import java.util.Enumeration;
import java.util.Hashtable;

public class ProcessRFID implements Runnable {

    private DataPool m_dataPool;
    private NodeInfo m_myState;
    private Hashtable inRoom = new Hashtable();
    private String currentRFID;

    /** Creates a new instance of ProcessRFID */
    public ProcessRFID(DataPool dp, NodeInfo myconfig) {
        m_dataPool = dp;
        m_myState = myconfig;
    }

    public void run() {
        /*Checking for each DataItem, to see which one was produced*/
        DataItem t_dataItem;
        if ((t_dataItem = m_dataPool.getData(IDConstants.T_PROCESSRFID,
                IDConstants.D_RFID)) != null) {
            RFID recvdRFID = (RFID) t_dataItem.core();
            this.handleRFIDProduced(recvdRFID);
        } else if ((t_dataItem = m_dataPool.getData(IDConstants.T_PROCESSRFID,
                IDConstants.D_RFIDQUERYRESPONSE)) != null) {
            RFIDQueryResponse recvdRFIDQueryResponse = (RFIDQueryResponse) t_dataItem.core();
            this.handleRFIDQueryResponseProduced(recvdRFIDQueryResponse);
        }
    }

    private void handleRFIDProduced(RFID r_RFID) {
        int ID = r_RFID.getID();
        String RFID = r_RFID.getRFID();
        m_myState.debugPrint("[ProcessRFID]Got a ID " + ID + " and RFID "
                + RFID);
        if (inRoom.containsKey(RFID)) {
            inRoom.remove(RFID);
            actuate();
        } else {
            currentRFID=RFID;
            RFIDLightSetting m_RFIDLightSetting = new RFIDLightSetting();
            m_RFIDLightSetting.setID(this.m_myState.getMyId());
            RFIDQueryRequest m_RFIDQueryRequest = new RFIDQueryRequest();
            m_RFIDQueryRequest.setID(this.m_myState.getMyId());
            m_RFIDQueryRequest.setRFID(RFID);
            DataItem m_dataitem = new DataItem(IDConstants.D_RFIDQUERYREQUEST,
                    IDConstants.T_PROCESSRFID, m_RFIDQueryRequest);
            m_dataPool.putData(m_dataitem);
        }
        
    }

    private void actuate() {

        int lightSetting;
        if(inRoom.isEmpty())
        {
            lightSetting=Constants.DEFAULT_SETTING;
        }
        else
        {
            lightSetting=0;
            Enumeration en=inRoom.elements();
            while(en.hasMoreElements())
            {
                int value=((Integer)en.nextElement()).intValue();
                if(value > lightSetting)
                {
                    lightSetting=value;
                }
            }
        }
        RFIDLightSetting m_RFIDLightSetting = new RFIDLightSetting();
        m_RFIDLightSetting.setID(this.m_myState.getMyId());
        m_RFIDLightSetting.setLightSetting(lightSetting);
        DataItem m_dataitem = new DataItem(IDConstants.D_RFIDLIGHTSETTING,
                IDConstants.T_PROCESSRFID, m_RFIDLightSetting);
        m_dataPool.putData(m_dataitem);
    }

    private void handleRFIDQueryResponseProduced(RFIDQueryResponse r_RFIDQueryResponse) {

        int ID = r_RFIDQueryResponse.getID();
        int lightSetting = r_RFIDQueryResponse.getLightSetting();
        m_myState.debugPrint("[ProcessRFID]Got a ID" + ID
                + " and Light Reading " + lightSetting);
        inRoom.put(currentRFID, new Integer(lightSetting));
        actuate();

    }
}
